﻿using System;


namespace SmartCA.Infrastructure.DomainBase
{
    /// <summary>
    /// 底层Entity
    /// </summary>
    public abstract class EntityBase: IEntity
    {
        private object key;

        /// <summary>
        /// Default Constructor
        /// </summary>
        protected EntityBase() : this(null)
        {
        }

        /// <summary>
        /// Overload constructor
        /// </summary>
        /// <param name="key"></param>
        protected EntityBase(object key)
        {
            this.key = key;
        }

        /// <summary>
        /// 主标识，注意大写
        /// </summary>
        public object Key
        {
            get { return key; }
        }
    

        #region Equality Tests

        /// <summary>
        /// 判断传入的实体是否和当前的实例相等
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public override bool Equals(object entity)
        {
            if (entity == null || !(entity is EntityBase)) return false;
            return this == (EntityBase)entity;
        }

        /// <summary>
        /// 重载操作符==
        /// </summary>
        /// <param name="base1"></param>
        /// <param name="base2"></param>
        /// <returns></returns>
        public static bool operator ==(EntityBase base1, EntityBase base2)
        {
            if ((object)base1 == null && (object)base2 == null)
                return true;

            if ((object)base1 == null || (object)base2 == null)
                return false;

            if (base1.key != base2.key) return false;

            return true;
        }

        public static bool operator !=(EntityBase base1, EntityBase base2)
        {
            return !(base1 == base2);
        }

        public override int GetHashCode()
        {
            return this.key.GetHashCode();
        }

        #endregion
    }
}
